package com.pengtu.gsj.controller.base;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.app.Trajectory;
import com.pengtu.gsj.entity.app.User;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckProjectService;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.service.system.TrajectoryService;
import com.pengtu.gsj.utils.UserUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

public class CheckTableBaseController {

	@Autowired
	private CheckProjectService checkProjectService;
	@Autowired
	private SysmenuManagerService sysmenuManagerService;
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private TrajectoryService trajectoryService;
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));    
	        binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));    
	        binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));  
		    binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));    
	}
	
	@ModelAttribute
	public void load(String projectId, String menuId, Model model) throws Exception {
		CheckProject checkProject;
		Sysmenu sysmenu;
		CheckTable checkTable;
		if (StringUtils.isNotEmpty(projectId)) {
			checkProject = checkProjectService.getCheckProject(projectId);
			model.addAttribute("projectId", projectId);
		} else {
			checkProject = new CheckProject();
		}
		if (StringUtils.isNotEmpty(menuId)) {
			sysmenu = sysmenuManagerService.get(menuId);
		} else {
			sysmenu = new Sysmenu();
		}
		String left = SpringMvcUtils.getParameter("left");
		model.addAttribute("menuList", checkProject.getMenuList());
		model.addAttribute("left", left);
		model.addAttribute("parentId", SpringMvcUtils.getParameter("parentId"));
		model.addAttribute("leftChild", SpringMvcUtils.getParameter("leftChild"));
		model.addAttribute("top", SpringMvcUtils.getParameter("top"));
		model.addAttribute("checkProject", checkProject);
		model.addAttribute("sysmenu", sysmenu);
		
		if (StringUtils.isNotEmpty(left)) {
			Sysmenu checkModeule = sysmenuManagerService.get(left);
			if (checkModeule != null) {
				model.addAttribute("moduleName", checkModeule.getName());
			}
		}
		if (StringUtils.isNotEmpty(menuId)) {
			model.addAttribute("menuId", menuId);
			if (!sysmenu.getParent().getId().equals(SpringMvcUtils.getParameter("left"))) {
				model.addAttribute("pid", sysmenu.getParent().getId());
			}
			
			model.addAttribute("childMenuList", Sysmenu.sort(sysmenu.getChildList()));
			if (StringUtils.isNotEmpty(projectId)) {
				QueryFilter queryFilter = new QueryFilter();
				queryFilter.addFilter("menuId", menuId, MatchType.EQ);
				queryFilter.addFilter("checkProject.id", projectId, MatchType.EQ);
				if (checkTableService.getCheckTables(queryFilter).size() == 0) {
					checkTable = new CheckTable();
				} else {
					checkTable = checkTableService.getCheckTables(queryFilter).get(0);
				}
			} else {
				checkTable = new CheckTable();
			}
		} else {
			checkTable = new CheckTable();
		}
		model.addAttribute("checkTable", checkTable);
		
		//绑定轨迹
		//得到当前用户
		User user = UserUtils.getUser();
		StringBuffer href = new StringBuffer();
		if (StringUtils.isNotEmpty(user.getId()) && StringUtils.isNotEmpty(menuId)) {
			//查询当前用户  当前菜单  在这一天是否已经访问过
			List<Trajectory> trajectories = trajectoryService.getTrajectoriesByUserId(menuId, user.getId());
			Trajectory trajectory = new Trajectory();
			//若该轨迹已经存在
			if (trajectories.size() > 0) {
				trajectory = trajectories.get(0);
			} else {
				//改轨迹不存在  则添加到数据库
				trajectory.setSysmenuId(sysmenu.getId());
				trajectory.setSysmenuName(sysmenu.getName());
				trajectory.setUserId(user.getId());
				trajectory.setUserName(user.getName());
				href.append(sysmenu.getCheckHref());
				if (sysmenu.getCheckHref().indexOf("?")>-1) {
					href.append("&");
				} else {
					href.append("?");
				}
				if (sysmenu.getHref().indexOf("left=") == -1) {
					href.append("left=" + SpringMvcUtils.getParameter("left"));
				}
				if (sysmenu.getHref().indexOf("top=") == -1) {
					href.append("&top=" + SpringMvcUtils.getParameter("top"));
				}
				if (sysmenu.getHref().indexOf("menuId=") == -1) {
					href.append("&menuId=" + SpringMvcUtils.getParameter("menuId"));
				}
				if (sysmenu.getHref().indexOf("leftChild=") == -1) {
					href.append("&leftChild=" + SpringMvcUtils.getParameter("leftChild"));
				}
				if (sysmenu.getHref().indexOf("projectId=") == -1) {
					href.append("&projectId=" + SpringMvcUtils.getParameter("projectId"));
				}
				trajectory.setOperateHref(href.toString());
				trajectory.setRemark("国省检");
			}
			trajectory.setOperateTime(new Date());
			trajectoryService.save(trajectory);
		}
	}
	
	public void attributesParams(CheckTable checkTable, RedirectAttributes attributes) {
		attributes.addFlashAttribute("msg", "保存/更新成功！");
		attributes.addAttribute("top",	SpringMvcUtils.getParameter("top"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		attributes.addAttribute("projectId", checkTable.getCheckProject().getId());
		attributes.addAttribute("menuId", checkTable.getMenuId());
		attributes.addAttribute("leftChild", SpringMvcUtils.getParameter("leftChild"));
	}
}
